<?php
$S_MERCHANT_ID='sandbox server id';
$S_MERCHANT_KEY='sandbox server key';
$P_MERCHANT_ID='production server id';
$P_MERCHANT_KEY='production server key';

$defaultheader='<?xml version="1.0" encoding="UTF-8"?>';

/* // directions
0) define merchant id, merchant key
1) Create $xml, use xmlTop() and xmlBot() to create open/close of xml. xmlItem() for each item in your cart.
2) curl it! - specify 
4) parse the curl result for serial # and redirect-url

// example of how to use:

//usage: 
// $items=xmlItem('title','description',$price,$quantity); // concatenate as many as you want
// $xml=xmlTop().$items.xmlBot();

// using the above format, this will send the shopping cart data of four quirky items to google checkout
$xml=xmlTop().xmlItem('Appleberry Crispz','funky applethangs that melt virtually in your mouth!',1,10).xmlItem('Blueberry Blisset','get hacked to virtual oblivion by blueberries in the Carribean!',0.50,5).xmlItem('Cranberry Chickens','chickens that taste like virtual chickens',21,1).xmlItem('dowager dame virtua','virtual dowagers on a stik',1000,5).xmlBot();

$xml=curlIt($S_MERCHANT_ID,$S_MERCHANT_KEY,$xml);
$xml=(parsedArray(parseReturn($xml)));
echo $xml[1]; // this is the redirect-url

*/



function hexdectags($t){
	return str_replace('>','&#x3e;',str_replace('<','&#x3c;',str_replace('&','&#x26;',$t)));
}

function amp2url($u){
	return str_replace('&amp;','&',$u);
}

function getauthkey($MERCHANT_ID,$MERCHANT_KEY){
	return base64_encode($MERCHANT_ID.':'.$MERCHANT_KEY);
}

function geturls(){global $S_MERCHANT_ID; // URL to curl for sandbox environment
	return 'https://sandbox.google.com/checkout/api/checkout/v2/merchantCheckout/Merchant/'.$S_MERCHANT_ID;
}
function geturlp(){global $P_MERCHANT_ID; // URL to curl for production environment
	return 'https://checkout.google.com/api/checkout/v2/merchantCheckout/Merchant/'.$P_MERCHANT_ID;
}

// process return XML +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function parseReturn($xml){ // formats return xml in php-friendly way
	return str_replace('</redirect-url>','</redirecturl>',str_replace('<redirect-url>','<redirecturl>',$xml)); 
}
function parsedArray($xmlparsed){ // spits out array: serial-number, redirect-url
	$xmlparsed=new SimpleXMLElement($xmlparsed);
	return array((string)$xmlparsed['serial-number'],amp2url($xmlparsed->redirecturl));
}
// process return XML +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// formats XML ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// example $xml=xmlTop().xmlItems('name','desc',1,5).xmlBot();
function xmlItem($name,$desc,$price,$quantity){
	return '      <item>
        <item-name>'.$name.'</item-name>
        <item-description>'.$desc.'</item-description>
        <unit-price currency="USD">'.$price.'</unit-price>
        <quantity>'.$quantity.'</quantity>
		<digital-content>
          <email-delivery>true</email-delivery>
        </digital-content>              
      </item>';
}
function xmlTop(){ // top part of xmlout before content
return '<?xml version="1.0" encoding="UTF-8"?>

<checkout-shopping-cart xmlns="http://checkout.google.com/schema/2">
  <shopping-cart>
    <items>';
}
function xmlBot(){ // bottom part of xmlout after content
return '    </items>
  </shopping-cart>
</checkout-shopping-cart>';
}
// formats XML ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function curlIt($MERCHANT_ID,$MERCHANT_KEY,$xml,$sandbox=1){ // curl to poke their api. $xml is xml you send. returns result
	$ch=curl_init();
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml; charset=UTF-8','Accept: application/xml; charset=UTF-8','Authorization: Basic '.getauthkey($MERCHANT_ID,$MERCHANT_KEY)));
	if($sandbox==1)curl_setopt($ch, CURLOPT_URL, geturls());
	else curl_setopt($ch, CURLOPT_URL, geturlp());
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERPWD, $MERCHANT_ID.':'.$MERCHANT_KEY);
	curl_setopt($ch, CURLOPT_TIMEOUT, 4);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
	$result=curl_exec($ch);
	curl_close($ch);
	return $result;
}



?>